package com.qq.employee.controller;



import com.qq.employee.service.EmployeeService;
import com.qq.employee.utity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/emp")

public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    @RequestMapping("/selectStaffByEmployeeId")
    public Employee selectStaffByEmployeeId(){
        return employeeService.selectStaffByEmployeeId();
    }

    @RequestMapping("/emps")
    public String listEmp(Model model){
        List<Employee> listEmp = employeeService.listEmp();
        model.addAttribute("emps",listEmp);
        return "emp/list";
    }
    @RequestMapping("/downloads")
    public String download(Model model){
        List<Employee> downloadEmp = employeeService.listEmp();
        model.addAttribute("download",downloadEmp);
        return "emp/table";
    }
    @RequestMapping("/insert")
    public String insertEmployee(Employee employee){
        employeeService.insertEmployee(employee);
        return "redirect:/emp/emps";
    }
    @RequestMapping("/update")
    public String updateEmployee(Employee employee) {
         employeeService.updateEmployee(employee);
        return "redirect:/emp/emps";

    }

    @PostMapping("/emp/{id}")
    public String deleteEmployee(@PathVariable("id") Integer id) {
         employeeService.deleteEmployee(id);
        return "redirect:/emp/emps";
    }
    @GetMapping("/emp")
    public String toAddPage(){
        return "emp/add";
    }



    @GetMapping("/emp/{id}")
    public String toUpdatePage(@PathVariable("id") Integer id,Model model){
        Employee employee = employeeService.get(id);
        model.addAttribute("emp",employee);
        return "emp/edit";
    }


}
